package com.ruoyi.system.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.service.DictService;
import com.ruoyi.system.domain.DoctorServer;
import com.ruoyi.system.domain.DutyInfo;
import com.ruoyi.system.service.IDoctorInfoService;
import com.ruoyi.system.service.IDoctorServerService;
import com.ruoyi.system.service.IDutyInfoService;
import com.ruoyi.system.service.IServerTypeService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.util.Date;
import java.util.List;

/**
 * 值日规划Controller
 * 
 * @author ruoyi
 * @date 2021-11-11
 */
@Controller
@RequestMapping("/duty/info")
public class DutyInfoController extends BaseController
{
    private String prefix = "duty/info";
    @Autowired
    private DictService dictService;

    @Autowired
    private IDutyInfoService dutyInfoService;

    @Autowired
    private IDoctorInfoService doctorInfoService;

    @Autowired
    private IDoctorServerService doctorServerService;

    @Autowired
    private IServerTypeService serverTypeService;

    @RequiresPermissions("duty:info:view")
    @GetMapping()
    public String info()
    {
        return prefix + "/info";
    }

    /**
     * 查询值日规划列表
     */
    @RequiresPermissions("duty:info:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(DutyInfo dutyInfo)
    {
        startPage();
        List<DutyInfo> list = dutyInfoService.selectDutyInfoList(dutyInfo);
        return getDataTable(list);
    }

    @PostMapping("/list2")
    @ResponseBody
    public AjaxResult list2(DutyInfo dutyInfo)
    {
        List<DutyInfo> list = dutyInfoService.selectDutyInfoList(dutyInfo);
        return AjaxResult.success(list);
    }



    /**
     * 导出值日规划列表
     */
    @RequiresPermissions("duty:info:export")
    @Log(title = "值日规划", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(DutyInfo dutyInfo)
    {
        List<DutyInfo> list = dutyInfoService.selectDutyInfoList(dutyInfo);
        ExcelUtil<DutyInfo> util = new ExcelUtil<DutyInfo>(DutyInfo.class);
        return util.exportExcel(list, "值日规划数据");
    }

    /**
     * 新增值日规划
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存值日规划
     */
    @RequiresPermissions("duty:info:add")
    @Log(title = "值日规划", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(DutyInfo dutyInfo)
    {
        Long dd = dutyInfo.getDay().getTime();
        dutyInfo.setDay(new Date(dd));
        DoctorServer doctorServer = new DoctorServer();
        doctorServer.setPid(dutyInfo.getPid());
        List<DoctorServer> list = doctorServerService.selectDoctorServerList(doctorServer);
        int judge = 0;
        for (DoctorServer doctorServer1:list) {
            dutyInfo.setSid(doctorServer1.getSid());
            judge = dutyInfoService.insertDutyInfo(dutyInfo);
        }
        return toAjax(judge);
    }

    /**
     * 修改值日规划
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        DutyInfo dutyInfo = dutyInfoService.selectDutyInfoById(id);
        mmap.put("dutyInfo", dutyInfo);
        return prefix + "/edit";
    }

    /**
     * 修改保存值日规划
     */
    @RequiresPermissions("duty:info:edit")
    @Log(title = "值日规划", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(DutyInfo dutyInfo)
    {
        return toAjax(dutyInfoService.updateDutyInfo(dutyInfo));
    }

    /**
     * 删除值日规划
     */
    @RequiresPermissions("duty:info:remove")
    @Log(title = "值日规划", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(dutyInfoService.deleteDutyInfoByIds(ids));
    }

    /**
     * 删除值日规划
     */
    @RequiresPermissions("duty:info:remove")
    @Log(title = "值日规划", businessType = BusinessType.DELETE)
    @PostMapping( "/remove2")
    @ResponseBody
    public AjaxResult remove(DutyInfo dutyInfo)
    {
        return toAjax(dutyInfoService.deleteDutyInfoByTimePidDay(dutyInfo));
    }

    @Log(title = "定时删除", businessType = BusinessType.DELETE)
    @PostMapping( "/remove3")
    @ResponseBody
    public AjaxResult removeOnTime() throws ParseException {
        return toAjax(dutyInfoService.deleteDutyInfoByDay());
    }





}
